package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐
     * @param dto
     */
    @Override
    @Transactional  // 开启事物，因为涉及多张表的操作。
    public void add(SetmealDTO dto) {
        // 1.添加套餐
        Setmeal setmeal = new Setmeal();
        // 复制属性
        BeanUtils.copyProperties(dto, setmeal);
        // 新增套餐状态为停售
        setmeal.setStatus(StatusConstant.DISABLE);
        setmealMapper.insert(setmeal);
        log.info("添加套餐-Id：{}", setmeal.getId());

        // 2.添加套餐的菜品
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(setmealDish);
        });
    }

    /**
     * 分页查询套餐
     * @param dto
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO dto) {
        // 1.设置分页参数
        PageHelper.startPage(dto.getPage(), dto.getPageSize());

        // 2.调用mapper的Page查询方法，并强转返回类型为Page
        Page<SetmealVO> page = setmealMapper.page(dto);

        // 3.封装PageResult对象返回
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        // 1.开启事务
        // 2.判断套餐状态，起售则提示错误信息
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                // 抛出业务异常
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });
        // 如果上面循环出现异常，下面就不会执行
        // 3.批量删除套餐
        setmealMapper.deleteBatch(ids);

        // 4.删除套餐菜品关系表中的数据
        setmealDishMapper.deleteBatchBySetmealId(ids);
    }

    @Override
    public List<Setmeal> list(Long categoryId, Integer enable) {
        return setmealMapper.list(categoryId, enable);
    }

    /**
     * 根据套餐id查询套餐菜品概要
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);

//        // 1.根据套餐id查询套餐的菜品列表
//        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
//
//        // 2.构造菜品列表
//        List<DishItemVO> dishItemVOS = new ArrayList<>();
//
//        // 3.循环setmealDishes，获取菜品id信息
//        for (SetmealDish setmealDish : setmealDishes) {
//            DishItemVO dishItemVO = new DishItemVO();
//            dishItemVO.setCopies(setmealDish.getCopies());
//            dishItemVO.setName(setmealDish.getName());
//            Dish dish = dishMapper.getById(Math.toIntExact(setmealDish.getDishId()));
//            if (dish != null) {
//                dishItemVO.setImage(dish.getImage());
//                dishItemVO.setDescription(dish.getDescription());
//            }
//            dishItemVOS.add(dishItemVO);
//        }
//
//        return dishItemVOS;
    }
}
